<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/9
 * Time: 14:35
 */
require 'vendor/autoload.php';
require 'connect.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\PDO\Database;
use Slim\PDO\Statement;
use Slim\PDO\Statement\SelectStatement;
$app = new \Slim\App();

$app->post('/addLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $line_id=$body->line_id;
    $city_id=$body->city_id;
    $price=$body->price;
    $cost=$body->cost;
    if($line_id!=null||$line_id!=""){
        if($city_id!=null||$city_id!=""){
            if($price!=null||$price!=""){
                if($cost!=null||$cost!=""){
                $selectStatement = $database->select()
                    ->from('line_city')
                    ->where('line_id','=',$line_id)
                    ->where('city_id','=',$city_id);
                $stmt = $selectStatement->execute();
                $data3 = $stmt->fetch();
                if($data3==null){
                    $insertStatement = $database->insert(array('line_id','city_id','price','cost'))
                        ->into('line_city')
                        ->values(array($line_id,$city_id,$price,$cost));
                    $insertId = $insertStatement->execute(false);
                    return $response->withJson(array("result" => "0", "desc" => "success"));
                }else{
                    return $response->withJson(array("result"=>"3","desc"=>"该线路已覆盖该城市"));
                }
                }else{
                    return $response->withJson(array("result"=>"5","desc"=>"缺少cost"));
                }
            }else{
                return $response->withJson(array("result"=>"2","desc"=>"缺少price"));
            }
        }else{
            return $response->withJson(array("result"=>"1","desc"=>"缺少city_id"));
        }
    }else{
        return $response->withJson(array("result"=>"4","desc"=>"缺少line_id"));
    }
});


$app->options('/alterLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "PUT");
    return $response;
});

$app->put('/alterLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $body = $request->getBody();
    $body=json_decode($body);
    $line_id=$body->line_id;
    $city_id=$body->city_id;
    $price=$body->price;
    $cost=$body->cost;
    $id=$body->id;
    if($id!=null||$id!=""){
        if($line_id!=null||$line_id!=""){
            if($price!=null||$price!=""){
                if($cost!=null||$cost!=""){
                    if($city_id!=null||$city_id!=""){
                        $selectStatement = $database->select()
                            ->from('line_city')
                            ->where('city_id','=',$city_id)
                            ->where('line_id','=',$line_id)
                            ->where('id','!=',$id);
                        $stmt = $selectStatement->execute();
                        $data3 = $stmt->fetch();
                        if($data3==null){
                            $updateStatement = $database->update(array('line_id'=>$line_id,'price'=>$price,'cost'=>$cost,'city_id'=>$city_id))
                                ->table('line_city')
                                ->where('id','=',$id);
                            $affectedRows = $updateStatement->execute();
                            return $response->withJson(array("result" => "0", "desc" => "success"));
                        }else{
                            return $response->withJson(array("result"=>"5","desc"=>"线路已经存在"));
                        }
                    }else{
                        return $response->withJson(array("result"=>"6","desc"=>"缺少city_id"));
                    }
                }else{
                    return $response->withJson(array("result"=>"4","desc"=>"缺少lowest_cost"));
                }
            }else{
                return $response->withJson(array("result"=>"3","desc"=>"缺少lowest_price"));
            }
        }else{
            return $response->withJson(array("result"=>"2","desc"=>"缺少province_id"));
        }
    }else{
        return $response->withJson(array("result"=>"1","desc"=>"缺少id"));
    }
});

$app->get('/getLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $id=$request->getParam('id');
    if($id!=null||$id!=""){
        $selectStatement = $database->select()
            ->from('line_city')
            ->where('id','=',$id);
        $stmt = $selectStatement->execute();
        $data = $stmt->fetch();
        return $response->withJson(array("result" => "0", "desc" => "success",'line_city'=>$data));
    }else{
        return $response->withJson(array("result" => "1", "desc" => "缺少id"));
    }
});

$app->options('/deleteLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $response=$response->withAddedHeader("Access-Control-Allow-Methods", "DELETE");
    return $response;
});

$app->delete('/deleteLineCity',function(Request $request,Response $response){
    $response=$response->withAddedHeader('Access-Control-Allow-Origin','*');
    $response=$response->withAddedHeader('Content-Type','application/json');
    $database=localhost();
    $id=$request->getParam('id');//获取请求路径后数据
    $deleteStatement = $database->delete()
        ->from('line_city')
        ->where('id', '=', $id);
    $affectedRows = $deleteStatement->execute();
    return $response->withJson(array("result" => "0", "desc" => "success"));
});


$app->run();

function localhost()
{
    return connect();
}

function encode($string , $skey ) {
    $strArr = str_split(base64_encode($string));
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key].=$value;
    return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
}

//解密
function decode($string, $skey) {
    $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    return base64_decode(join('', $strArr));
}

?>